package com.wing.res.service.impl;


import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wing.common.enums.MaintenanceEnum;
import com.wing.common.enums.ProductEnum;
import com.wing.common.model.entity.ExternalProduct;
import com.wing.common.service.ExternalProductService;
import com.wing.common.utils.UniqueIdGeneratorUtil;
import com.wing.res.dao.ResProBomRecordDao;
import com.wing.res.model.entity.ResProBomRecord;
import com.wing.res.model.entity.ResProTask;
import com.wing.res.model.response.ResProTaskRecordSearchVo;
import com.wing.res.service.ResProBomRecordService;
import com.wing.res.service.ResProTaskService;
import com.wing.sell.model.entity.CustomersInfo;
import com.wing.sell.model.entity.OrderProEnclosure;
import com.wing.sell.service.CustomersInfoService;
import com.wing.sell.service.OrderProEnclosureService;
import com.wing.web.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * skuBom管理-ServiceImpl
 *
 * @version 1.1.0
 * @Author hz
 * @Date 2024-09-23 20:59
 **/
@Service
public class ResProBomRecordServiceImpl extends ServiceImpl<ResProBomRecordDao, ResProBomRecord> implements ResProBomRecordService {


    @Autowired
    private ResProTaskService resProTaskService;

    @Autowired
    private CustomersInfoService customersInfoService;

    @Autowired
    private ExternalProductService externalProductService;

    @Autowired
    private OrderProEnclosureService orderProEnclosureService;

    /**
     * 点击确认
     * @param id
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int confirm(String id) {
        //1. 修改状态
        ResProTask byId = resProTaskService.getById(id);
        byId.setModifier(JwtUtils.getLoginUserId());
        byId.setModifierName(JwtUtils.getLoginUsername());
        byId.setUpdateTime(LocalDateTime.now());
        byId.setIsBom(ProductEnum.dictEmum.S.getCode());
        if (byId.getIsCraftsmanship().equals(ProductEnum.dictEmum.S.getCode())) {
            byId.setStatus(MaintenanceEnum.Task_Status_Enum.DJD.getCode());
        }
        boolean updateById = this.resProTaskService.updateById(byId);
        if (updateById) {
            //添加记录
            ResProBomRecord resProBomRecord = new ResProBomRecord();
            resProBomRecord.setCreateTime(LocalDateTime.now());
            resProBomRecord.setCreatorName(JwtUtils.getLoginUsername());
            resProBomRecord.setCreator(JwtUtils.getLoginUserId());
            // 获取当前日期
            LocalDate currentDate = LocalDate.now();
            // 获取当前年份
            int currentYear = currentDate.getYear();
            resProBomRecord.setVersionName("ver."+currentYear+"-"+1);
            resProBomRecord.setTaskId(byId.getId());
            resProBomRecord.setRemark("bom点击确认自动添加!");
            String code = UniqueIdGeneratorUtil.generateUniqueId("BOM", "yyyyMMdd", 1000, 9999);
            resProBomRecord.setCode(code);
            //保存 记录
            boolean save = this.save(resProBomRecord);
            if (save) {
                return 1;
            }
        }
        return 0;
    }

    /**
     * 查询bom记录
     * @param pg
     * @param map
     * @return
     */
    @Override
    public Page<ResProTaskRecordSearchVo> pageList4app(Page<ResProTaskRecordSearchVo> pg, Map<String, Object> map) {
        List<ResProTaskRecordSearchVo> tasks = this.baseMapper.pageList4app(pg,map);

        tasks.forEach(info->{
            //如果类型是 客户订单  则需要查询客户信息
            if (info.getSource().equals(MaintenanceEnum.Task_Source_Enum.KHDD.getCode())){
                //客户
                CustomersInfo byId = customersInfoService.getById(info.getCustomerId());
                if (ObjectUtil.isNotEmpty(byId)){
                    info.setCustomerCode(byId.getCustomerName());
                }
                ExternalProduct externalProduct = externalProductService.getById(info.getExtProductId());
                if (ObjectUtil.isNotEmpty(externalProduct)){
                    info.setCusProductName(externalProduct.getName());
                    info.setCusProductCode(externalProduct.getCode());
                }

                QueryWrapper<OrderProEnclosure> enclosureQueryWrapper = new QueryWrapper<>();
                enclosureQueryWrapper.eq("task_id",info.getId());
                OrderProEnclosure one = orderProEnclosureService.getOne(enclosureQueryWrapper);
                if (ObjectUtil.isNotEmpty(one)){
                    info.setFileUrl(one.getFileUrl());
                    info.setCusProductFigure(one.getFileName());
                }
            }
        });
        pg.setRecords(tasks);
        return pg;
    }
}
